//
//  CJNativeGMAdapter.m
//  CJModileGMAdapter
//
//  Created by shanghailingye on 2024/4/24.
//

#import "CJNativeGMAdapter.h"

@interface CJNativeGMAdapter()<CJNativeAdDelegate>

@property (nonatomic, strong) CJNativeAd *nativeAd;
@property (nonatomic, strong) UIView *contentView;

@end

@implementation CJNativeGMAdapter

- (void)loadNativeAdWithSlotID:(nonnull NSString *)slotID andSize:(CGSize)size imageSize:(CGSize)imageSize parameter:(nonnull NSDictionary *)parameter {
    self.nativeAd = [[CJNativeAd alloc] initWithSlotId:slotID size:size rootViewController:self.bridge.viewControllerForPresentingModalView];
    self.nativeAd.delegate = self;
    [self.nativeAd loadAdData:1];
}

- (void)registerContainerView:(nonnull __kindof UIView *)containerView andClickableViews:(nonnull NSArray<__kindof UIView *> *)views forNativeAd:(nonnull id)nativeAd {
    
}

- (void)renderForExpressAdView:(nonnull UIView *)expressAdView {
    
}

- (void)setRootViewController:(nonnull UIViewController *)viewController forExpressAdView:(nonnull UIView *)expressAdView {
    
}

- (void)setRootViewController:(nonnull UIViewController *)viewController forNativeAd:(nonnull id)nativeAd {
    
}

#pragma mark CJNativeAdDelegate
- (void)nativeExpressAdDidLoad:(id)nativeExpressAd views:(NSArray *)nativeExpressViews {
    NSInteger cpm = [self.nativeAd getECpm];
    NSDictionary *ext = @{
        BUMMediaAdLoadingExtECPM : cpm ? [NSString stringWithFormat:@"%ld", cpm] : @"0",
    };
    if (nativeExpressViews.count > 0 && [nativeExpressViews.firstObject isKindOfClass:[UIView class]]) {
        self.contentView = nativeExpressViews.firstObject;
    }
    [self.bridge nativeAd:self didLoadWithExpressViews:nativeExpressViews exts:@[ext]];
}

- (void)nativeExpressAdDidRender:(nonnull id)nativeExpressView {
    
}

- (void)nativeExpressAdLoadFailed:(nonnull id)nativeExpressAd error:(nonnull NSError *)error {
    [self.bridge nativeAd:self didLoadFailWithError:error];
}

- (void)nativeExpressAdOnClicked:(nonnull id)nativeExpressView {
    [self.bridge nativeAd:self didClickWithMediatedNativeAd:self.contentView];
}

- (void)nativeExpressAdOnClosed:(nonnull id)nativeExpressView {
    [self.bridge nativeAd:self didCloseWithExpressView:self.contentView closeReasons:@[]];
}

- (void)nativeExpressAdOnShow:(nonnull id)nativeExpressView {
    [self.bridge nativeAd:self didVisibleWithMediatedNativeAd:self.contentView];
}

@end
